package com.wanmait.zaisousuo.service.Impl;

import com.github.pagehelper.PageInfo;
import com.wanmait.zaisousuo.mapper.CompanyApprovalMapper;
import com.wanmait.zaisousuo.pojo.CompanyApproval;
import com.wanmait.zaisousuo.service.UserinfoService;
import com.wanmait.zaisousuo.util.CodeUtil;
import com.wanmait.zaisousuo.util.MD5Utils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import com.wanmait.zaisousuo.mapper.UserinfoMapper;
import com.wanmait.zaisousuo.pojo.Userinfo;
import org.springframework.transaction.annotation.Transactional;

@Service("UserinfoService")
@Transactional
public class UserinfoServiceImpl implements UserinfoService {

    @Resource
    private UserinfoMapper userinfoMapper;

    @Resource
    private CompanyApprovalMapper companyApprovalMapper;

    @Override
    public void insert(Userinfo userinfo) {
        userinfo.setNickname("用户"+CodeUtil.randomNickname());
        userinfoMapper.register(userinfo);
    }

    @Override
    public void update(Userinfo userinfos) {
        userinfoMapper.update(userinfos);
    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public Userinfo findById(Integer id) {
        Userinfo userinfo = userinfoMapper.selectByPrimaryKey(id);
        return userinfo;
    }

    @Override
    public List<Userinfo> findAll() {
        return userinfoMapper.findAll();
    }

    @Override
    public List<Userinfo> findAll(Userinfo object) {
        return null;
    }

    @Override
    public int getDataCount() {
        return 0;
    }

    @Override
    public int getDataCount(Userinfo object) {
        return 0;
    }

    @Override
    public PageInfo<Userinfo> findList(int pageNum) {
        return null;
    }

    @Override
    public Userinfo findByEmail(String email) {
        return userinfoMapper.findByEmail(email);
    }

    @Override
    public Userinfo login(String username, String password) {
        return userinfoMapper.login(username,password);
    }

    @Override
    public Userinfo findByEmailAndPhone(String email,String phoneNumber) {
        return userinfoMapper.findByEmailAndPhone(email, phoneNumber);
    }

    @Override
    public Userinfo findPassword(String username) {
        return userinfoMapper.findPassword(username);
    }

    @Override
    public Userinfo findByPhone(String phoneNumber) {
        return userinfoMapper.findByPhone(phoneNumber);
    }

    @Override
    public void insertByPhone(Userinfo userinfo) {
        userinfo.setNickname("用户"+CodeUtil.randomNickname());
        userinfoMapper.insertByPhone(userinfo);
    }

    @Override
    public void updatePassword(String password, String username) {
        String passwords = MD5Utils.md5(password);
        userinfoMapper.updatePassword(passwords,username);
    }

    @Override
    public Userinfo checkNickname(String nickname) {
        return userinfoMapper.checkNickname(nickname);
    }

    @Override
    public void updateNickname(String nickname,Integer id) {
        userinfoMapper.updateNickname(nickname,id);
    }

    @Override
    public void updateEmail(String email,Integer id) {
        userinfoMapper.updateEmail(email,id);
    }

    @Override
    public void updatePhoneNumber(String phoneNumber, Integer id) {
        userinfoMapper.updatePhoneNumber(phoneNumber,id);
    }

    @Override
    public void modPassword(String password, Integer id) {
        userinfoMapper.modPassword(password,id);
    }

    @Override
    public void updateHeadPic(Userinfo userinfo) {
        userinfoMapper.updateHeadPic(userinfo);
    }

    @Override
    public void checkVip(Userinfo userinfo) {
            userinfoMapper.updateByPrimaryKey(userinfo);
    }

    //后台：查找所有VIP用户（包括未审核、审核通过、审核失败状态）
    @Override
    public List<Userinfo> findVip(Integer userStates) {
        return userinfoMapper.findVip(userStates);
    }

    @Override
    public String changeVipStatus(Integer id,Boolean flag) {
        Integer userTypeId = userinfoMapper.selectUserTypeId(id);
        if (userTypeId!=3)
        {
            userinfoMapper.updateUserType(id,flag);
        }
        userinfoMapper.updateVipStatus(id,flag);
        return id+"&split&"+flag;
    }

    @Override
    public List<Userinfo> findAllCompany(Integer userStates) {
        return userinfoMapper.findAllCompany(userStates);
    }

    //修改公司审核状态
    @Override
    public String changeCompanyStatus(Integer id, Boolean flag) {

        companyApprovalMapper.changeCompanyStates(id,flag); //修改审核状态
        userinfoMapper.updateUserType2(id,flag);//修改用户类型
        return id+"&split&"+flag;
    }

    @Override
    public Integer banUser(Integer id) {
        userinfoMapper.banUser(id);
        return id;
    }

    @Override
    public Integer recoverUser(Integer id) {
        userinfoMapper.recover(id);
        return  id;
    }


}
